﻿using System.Windows;
using System.Windows.Controls;

namespace Renren.Controls
{
    public partial class LoadingBox : UserControl
    {
        public static readonly DependencyProperty BusyTextProperty =
            DependencyProperty.Register(
            "BusyText",
            typeof(string),
            typeof(LoadingBox),
            new PropertyMetadata("正在更新...", TextChanged));

        public static readonly DependencyProperty IdleTextProperty =
            DependencyProperty.Register(
            "IdleText",
            typeof(string),
            typeof(LoadingBox),
            new PropertyMetadata("更新", TextChanged));

        public static readonly DependencyProperty ArrowUpTextProperty =
            DependencyProperty.Register(
            "ArrowUpText",
            typeof(string),
            typeof(LoadingBox),
            new PropertyMetadata("松开即可刷新", TextChanged));

        public static readonly DependencyProperty ArrowDownTextProperty =
            DependencyProperty.Register(
            "ArrowDownText",
            typeof(string),
            typeof(LoadingBox),
            new PropertyMetadata("下拉可以刷新", TextChanged));

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register(
            "State",
            typeof(LoadingState),
            typeof(LoadingBox),
            new PropertyMetadata(LoadingState.Busy));

        public string BusyText
        {
            get { return (string)GetValue(BusyTextProperty); }
            set { SetValue(BusyTextProperty, value); }
        }

        public string IdleText
        {
            get { return (string)GetValue(IdleTextProperty); }
            set { SetValue(IdleTextProperty, value); }
        }

        public string ArrowUpText
        {
            get { return (string)GetValue(ArrowUpTextProperty); }
            set { SetValue(ArrowUpTextProperty, value); }
        }

        public string ArrowDownText
        {
            get { return (string)GetValue(ArrowDownTextProperty); }
            set { SetValue(ArrowDownTextProperty, value); }
        }

        public LoadingState State
        {
            get { return (LoadingState)GetValue(StateProperty); }
            set { UpdateState(value); }
        }

        private void UpdateState(LoadingState newState)
        {
            switch (newState)
            {
                case LoadingState.Busy:
                    if (State != LoadingState.Busy)
                    {
                        this.TextBlock.Text = this.BusyText;
                        this.SpinningBall.Visibility = Visibility.Visible;
                        this.Arrow.Visibility = Visibility.Collapsed;
                        this.Spin.Begin();
                    }
                    break;
                case LoadingState.Idle:
                    this.TextBlock.Text = this.IdleText;
                    this.SpinningBall.Visibility = Visibility.Collapsed;
                    this.Arrow.Visibility = Visibility.Collapsed;
                    this.Spin.Stop();
                    break;
                case LoadingState.ArrowUp:
                    if (State != LoadingState.ArrowUp)
                    {
                        this.TextBlock.Text = this.ArrowUpText;
                        this.SpinningBall.Visibility = Visibility.Collapsed;
                        this.Arrow.Visibility = Visibility.Visible;
                        this.FlipUp.Begin();
                    }
                    break;
                case LoadingState.ArrowDown:
                    if (State != LoadingState.ArrowDown)
                    {
                        this.TextBlock.Text = this.ArrowDownText;
                        this.SpinningBall.Visibility = Visibility.Collapsed;
                        switch (State)
                        {
                            case LoadingState.Busy:
                                this.FlipTransform.Angle = 180;
                                this.Arrow.Visibility = Visibility.Visible;
                                break;
                            case LoadingState.ArrowUp:
                                this.FlipDown.Begin();
                                break;
                        }
                    }
                    break;
            }

            SetValue(StateProperty, newState);
        }

        static private void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var loadingBox = d as LoadingBox;
            loadingBox.UpdateState(loadingBox.State);
        }

        public LoadingBox()
        {
            InitializeComponent();
        }

        public enum LoadingState
        {
            Busy,
            Idle,
            ArrowUp,
            ArrowDown
        }
    }
}
